package com.example.football.service;

import com.example.football.dao.NewsDao;
import com.example.football.dao.PeopleDao;
import com.example.football.mapper.NewsMapper;
import com.example.football.mapper.ScheduleMapper;
import com.example.football.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class NewsServiceImpl implements NewsService{
    @Resource
    private NewsDao sd;

    @Override
    public List<News> queryAll() {

        return sd.selectAll();
    }

    @Override
    public void removeByNewsid(Integer id) {
        try {
            sd.deleteByNewsid(id);
        } catch (Exception e) {
            throw new RuntimeException("根据ID删除有异常");
        }
    }

    @Override
    public List<News> queryByTitle(String title) {

        return sd.selectByTitle(title);
    }

    @Override
    public News queryById(Integer id) {

        return sd.selectById(id);
    }

    @Override
    public void addNews(News news) {
        try {
            sd.insertNews(news);
        } catch (Exception e) {
            throw new RuntimeException("添加有异常");
        }
    }

    @Override
    public void changeNews(News news) {
//        try {
            sd.updateNews(news);
//        } catch (Exception e) {
//            throw new RuntimeException("修改数据异常");
//        }
    }

    @Autowired
    private NewsMapper newsMapper;

    public Result showNews(People id){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<News> news = newsMapper.findNews(id);
            result.setDetail(news);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
